#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

#export DH_VERBOSE=1

PROGRAMS = rubik skewb dino pyraminx oct mball cubes triangles hexagons mlink barrel panex

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CONFIGURE_FLAGS := --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
			--prefix=/usr --bindir=\$${prefix}/games --mandir=\$${prefix}/share/man \
			--with-x --with-xpm --without-rplay --with-esound --without-nas

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

build:
	true

build-motif-stamp:
	dh_testdir
	for PROG in $(PROGRAMS) ; do \
		set -e ; \
		cd x$$PROG ; \
		CFLAGS="$(CFLAGS)" ./configure $(CONFIGURE_FLAGS) --with-motif --enable-xm-prefix ; \
		$(MAKE) ; \
		cd .. ; \
	done
	touch $@

build-nomotif-stamp:
	dh_testdir
	-$(MAKE) -f xpuzzles.Makefile distclean
	for PROG in $(PROGRAMS) ; do \
		set -e ; \
		cd x$$PROG ; \
		CFLAGS="$(CFLAGS)" ./configure $(CONFIGURE_FLAGS) --without-motif ; \
		$(MAKE) ; \
		cd .. ; \
	done
	touch $@

clean:
	dh_testdir
	dh_testroot
	-$(MAKE) -f xpuzzles.Makefile distclean
	dh_clean build-motif-stamp build-nomotif-stamp debian/*.menu debian/*links  
	$(RM) -r debian/xdesktop debian/xmdesktop

install: install-motif-stamp install-nomotif-stamp
	for INFIX in '' m ; do \
		set -e ; \
		DOCDIR=$(CURDIR)/debian/x$${INFIX}puzzles/usr/share/doc/x$${INFIX}puzzles ; \
		PIXDIR=$(CURDIR)/debian/x$${INFIX}puzzles/usr/share/pixmaps ; \
		install -D -m 0644 xpuzzles.README $$DOCDIR/README ; \
		echo -n "" >  debian/x$${INFIX}puzzles.menu; \
		mkdir -p $(CURDIR)/debian/x$${INFIX}desktop; \
		for PROG in $(PROGRAMS) ; do \
			install -D -m 0644 x$$PROG/README $$DOCDIR/x$$INFIX$$PROG.README ; \
			install -D -m 0644 x$$PROG/icons/$$PROG.t.xpm $$PIXDIR/x$$PROG.t.xpm ; \
			install -D -m 0644 x$$PROG/icons/$$PROG.m.xpm $$PIXDIR/x$$PROG.m.xpm ; \
			echo '?package(x'$$INFIX'puzzles): needs="X11" \' >> debian/x$${INFIX}puzzles.menu ; \
			echo '  section="Games/Puzzles" title="'x$$INFIX$$PROG'" \' >> debian/x$${INFIX}puzzles.menu ; \
			echo '  command="/usr/games/x'$$INFIX$$PROG'" \' >> debian/x$${INFIX}puzzles.menu ; \
			echo '  icon="/usr/share/pixmaps/x'$$PROG'.m.xpm"' >> debian/x$${INFIX}puzzles.menu ; \
			echo '[Desktop Entry]' > debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'Name=x'$$INFIX$$PROG'' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'GenericName='$$PROG' game' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'Exec=/usr/games/x'$$INFIX$$PROG'' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'Icon=/usr/share/pixmaps/x'$$PROG'.m.xpm' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'Terminal=false' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'Type=Application' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'Categories=X;Application;Game;' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
			echo 'Encoding=UTF-8' >> debian/x$${INFIX}desktop/x$${INFIX}$$PROG.desktop ; \
		done ; \
		mkdir -p $(CURDIR)/debian/x$${INFIX}puzzles/usr/share/applications; \
		cp debian/x$${INFIX}desktop/*.desktop $(CURDIR)/debian/x$${INFIX}puzzles/usr/share/applications; \
	done

install-motif-stamp: build-motif-stamp
	dh_testroot
	for PROG in $(PROGRAMS) ; do \
		set -e ; \
		cd x$$PROG ; \
		$(MAKE) install DESTDIR=$(CURDIR)/debian/xmpuzzles xapploaddir=/etc/X11/app-defaults ; \
		cd .. ; \
		echo 'usr/share/man/man6/x'$$PROG'.6.gz usr/share/man/man6/xm'$$PROG'.6.gz' >> debian/xmpuzzles.links ; \
	done

install-nomotif-stamp: build-nomotif-stamp
	dh_testroot
	for PROG in $(PROGRAMS) ; do \
		set -e ; \
		cd x$$PROG ; \
		$(MAKE) install DESTDIR=$(CURDIR)/debian/xpuzzles xapploaddir=/etc/X11/app-defaults ; \
		cd .. ; \
	done

binary-indep: install
# We have nothing to do by default.

binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_install -a
	dh_installdocs -a -A debian/README.Debian
	dh_installmenu -a
	dh_installman -a
	dh_installchangelogs -a
	dh_strip -a
	dh_link -a
	dh_desktop -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary
